// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Drawing;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.FeedCore.Helpers;
using svs.Mobile.BeyondMedia.Objects;

namespace svs.Mobile.BeyondMedia.Controls
{
	/// <summary>
	/// Provide a 2 line listview
	/// </summary>
	internal class KineticPlayListView : KineticTracksView
	{
		public event Helpers.TrackSelectedEvent TrackSelected;
		private string _PlaylistTitle = null;

		public KineticPlayListView()
		{
			FeedRepository.TrackChanged += OnTrackChanged;
			
			ShowSelectedItem = false;
			
		}

		protected override string IfEmptyText
		{
			get
			{
				return string.Empty;
			}
		}

		protected override void OnCustomizeControlSettings(UiCore.KineticControls.ListViewSettings settings)
		{
			settings.ListItemSeparatorColor = Color.Gainsboro;
			settings.ListItemSecondaryTextColor = Color.DarkGray;
			settings.ListItemBackgroundColorOdd = Color.Gainsboro;
			settings.ListItemBackgroundColorEven = Color.LightGray;

			settings.NoteFontSize = 12;
			settings.SecondaryIconWidth = 40;
		}

		protected override void OnKeyDown(KeyEventArgs e)
		{
				ShortcutKeyManager.HandleKey(e.KeyCode);
		}

		private void OnTrackChanged(Track aTrack)
		{
			UpdateItem(aTrack, aTrack.CurrentPlayState == Track.PlayMode.Play);
		}

		public void SetPlaylist(TrackList tracks)
		{
				Items.Clear();
				Items.AddRange(tracks);

				if (ViewManager.Tracks.HasFeedFilter())
					_PlaylistTitle = ViewManager.Tracks.GetFeedFilter().Name;
				else
					_PlaylistTitle = string.Format("All in '{0}'", Configuration.ActiveFeedCategory.Name);

				SetItems(BuildNativeControlItems(),-1,false);
		}


		protected override void OnTrackAdded(Track i_track)
		{
			// Ignore that here - the playlist has already been generated
		}

		protected override ListViewItemData BuiltNativeControlItem(Track track)
		{
			var i = base.BuiltNativeControlItem(track);

			i.Group = _PlaylistTitle;

			if ( track.CurrentPlayState == Track.PlayMode.Pause /*&& ViewManager.MediaPlayer.CurrentTrack != null && ViewManager.MediaPlayer.CurrentTrack.Equals(track)*/)
			{
				i.PrimaryImage = FeedImageList.PausedTrackImage;
			}

			if (track.IsFullyDownloaded)
			{
				i.SecondaryText = CoreHelper.FormatDate(track.LastModifiedDate);

				if(track.TotalTime > 0)
          i.Note = string.Format("{0}", track.TotalTimeAsString);
			}

			return i;
		}

		protected override void OnItemBodyClicked(int index)
		{
			if (TrackSelected != null)
				TrackSelected(SelectedTrack);
		}
	}
}